package mbp.gui.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.UIManager;

import mbp.base.FeatureType;
import mbp.base.GuiFunction;
import mbp.base.ResourceType;
import mbp.core.Resource;
import mbp.core.ResourceTypeBuilder;
import mbp.core.ResourceUtility;
import mbp.core.ResourceValidation;
import mbp.exceptions.InvalidFeatureNameException;
import mbp.exceptions.InvalidFeatureTypeException;
import mbp.exceptions.InvalidFeatureValueException;
import mbp.gui.frames.MbpManagerGui;
import mbp.gui.frames.ResourceEditor;
import mbp.gui.frames.ResourceUtilityEditor;
import mbp.gui.frames.ResourceValidationEditor;

import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JLabel;

import org.apache.log4j.Logger;

public class BusinessAgentGui extends JPanel implements ActionListener
{

	private MbpManagerGui mbpManagerGui;
	private JTextField txtAgentClass;
	private JTextField txtClient;
	private JButton btnStartAgent;
	private JButton btnCreateRc;
	private JButton btnCreateRcv;
	private JButton btnCreateRcu;
	
	private JComboBox cmbType;
	
	private ValidationListPanel validationListPanel;
	private ResourceListPanel resourceListPanel;
	private UtilityListPanel utilityListPanel;
	
	private List<ResourceValidation> rcvList;
	private List<Resource> rcList;
	private List<ResourceUtility> rcuList;
	
	private Logger logger = Logger.getLogger(this.getClass());
	private ResourceTypeBuilder rtb;
	
	public BusinessAgentGui(MbpManagerGui mbpManagerGui)
	{
		this.mbpManagerGui = mbpManagerGui;
		rcvList = new ArrayList<ResourceValidation>();
		rcList = new ArrayList<Resource>();
		rcuList = new ArrayList<ResourceUtility>();
		rtb = new ResourceTypeBuilder("");
		
		setLayout(null);
		
		JPanel panel_0 = new JPanel();
		panel_0.setLayout(null);
		panel_0.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Agent class and name", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 70, 213)));
		panel_0.setBounds(10, 11, 367, 69);
		add(panel_0);
		
		btnStartAgent = new JButton("Start");
		btnStartAgent.setFont(new Font("Dialog", Font.PLAIN, 11));
		btnStartAgent.setBounds(286, 25, 70, 23);
		panel_0.add(btnStartAgent);
		btnStartAgent.addActionListener(this);
		
		txtAgentClass = new JTextField();
		txtAgentClass.setText("mbp.agents.MbpClientAgent");
		txtAgentClass.setBounds(10, 25, 170, 23);
		panel_0.add(txtAgentClass);
		txtAgentClass.setColumns(10);
		
		txtClient = new JTextField();
		txtClient.setText("client1");
		txtClient.setBounds(190, 25, 86, 23);
		panel_0.add(txtClient);
		txtClient.setColumns(10);
		setBounds(10, 50, 739, 428);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Resources to achieve", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(387, 11, 310, 92);
		add(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		validationListPanel = new ValidationListPanel(){
			public void processDoubleClick(int index)
			{
				if(index != -1)
				{
					ResourceValidationEditor rcvEdit = new ResourceValidationEditor(rcvList.get(index), GuiFunction.EDITOR, this);
					rcvEdit.setVisible(true);
				}
			}
			
			public void processKeyPressed(int index)
			{
				if(index != -1)
				{
					rcvList.remove(index);
					this.reloadList();
				}
			}
		};
		panel_1.add(validationListPanel, BorderLayout.CENTER);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Available resources", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(387, 103, 310, 149);
		add(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		resourceListPanel = new ResourceListPanel(){
			public void processDoubleClick(int index)
			{
				if(index != -1)
				{
					ResourceEditor rcEdit = new ResourceEditor(rcList.get(index), GuiFunction.EDITOR, this);
					rcEdit.setVisible(true);
				}
			}
			
			public void processKeyPressed(int index)
			{
				if(index != -1)
				{
					rcList.remove(index);
					this.reloadList();
				}
			}
		};
		panel_2.add(resourceListPanel, BorderLayout.CENTER);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(null, "Utility functions", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_3.setBounds(387, 252, 310, 139);
		add(panel_3);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		utilityListPanel = new UtilityListPanel(){
			public void processDoubleClick(int index)
			{
				if(index != -1)
				{
					ResourceUtilityEditor rcuEdit = new ResourceUtilityEditor(rcuList.get(index), GuiFunction.EDITOR, this);
					rcuEdit.setVisible(true);
				}
			}
			
			public void processKeyPressed(int index)
			{
				if(index != -1)
				{
					rcuList.remove(index);
					this.reloadList();
				}
			}
		};
		panel_3.add(utilityListPanel);
		
		JPanel panel_4 = new JPanel();
		panel_4.setLayout(null);
		panel_4.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Resource Type Fom XML", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_4.setBounds(10, 91, 367, 80);
		add(panel_4);
		
		cmbType = new JComboBox();
		cmbType.setBounds(10, 39, 125, 23);
		panel_4.add(cmbType);
		
		btnCreateRc = new JButton("RC");
		btnCreateRc.setToolTipText("Available resources");
		btnCreateRc.setName("CREATE");
		btnCreateRc.setFont(new Font("Dialog", Font.PLAIN, 11));
		btnCreateRc.setBounds(215, 39, 60, 23);
		panel_4.add(btnCreateRc);
		btnCreateRc.addActionListener(this);
		
		JLabel label = new JLabel("Resource type");
		label.setBounds(10, 22, 100, 14);
		panel_4.add(label);
		
		btnCreateRcv = new JButton("RCV");
		btnCreateRcv.setToolTipText("Resources to achieve");
		btnCreateRcv.setFont(new Font("Dialog", Font.PLAIN, 11));
		btnCreateRcv.setBounds(145, 39, 60, 23);
		panel_4.add(btnCreateRcv);
		btnCreateRcv.addActionListener(this);
		
		btnCreateRcu = new JButton("RCU");
		btnCreateRcu.setToolTipText("Utility functions");
		btnCreateRcu.setFont(new Font("Dialog", Font.PLAIN, 11));
		btnCreateRcu.setBounds(285, 39, 60, 23);
		panel_4.add(btnCreateRcu);
		btnCreateRcu.addActionListener(this);
		
		

	}
	
	/**
	 * D
	 * @param rcv
	 */
	public void addResourceValidation(ResourceValidation rcv)
	{
		rcvList.add(rcv);
		validationListPanel.reloadListObj(rcvList);
	}
	
	public void addResource(Resource rc)
	{
		rcList.add(rc);
		resourceListPanel.reloadListObj(rcList);
	}
	
	public void addResourceUtility(ResourceUtility rcu)
	{
		rcuList.add(rcu);
		utilityListPanel.reloadListObj(rcuList);
	}
	
	public void loadResourceTypeList(List<String> types)
	{
		Iterator<String> it = types.iterator();
		
		while(it.hasNext())
		{
			String type = it.next();
			cmbType.addItem(type);
		}
	}
	
	public void setDefaultValues(Resource res)
	{
		Iterator it = res.getType().getFeaturesCollection().keySet().iterator();
		while (it.hasNext()) 
		{
			String key = (String)it.next();
			try 
			{
				FeatureType type = res.getFeatureType(key);
				switch(type)
				{
				case INT:
					res.setFeatureValue(key, 0);										
					break;
				case FLOAT:
					res.setFeatureValue(key, Float.parseFloat("0.0"));
					break;
				case ENUM:
					res.setFeatureValue(key, 0);
				default:				
					break;
				}
				
				res.setOwner(new String(txtClient.getText()));
			} 
			catch (InvalidFeatureNameException e) 
			{
				logger.error("InvalidFeatureNameException", e);
			} 
			catch (InvalidFeatureTypeException e) 
			{
				logger.error("InvalidFeatureTypeException", e);
			} 
			catch (InvalidFeatureValueException e) 
			{
				logger.error("InvalidFeatureValueException", e);
			}
			catch (Exception e)
			{
				logger.error("Exception", e);					
			}
			
		}

	}
	
//	public void LoadServiceTypeList(List<String> types)
//	{
//		Iterator<String> it = types.iterator();
//		
//		while(it.hasNext())
//		{
//			String type = it.next();
//			cmbTypesXml.addItem(type);
//		}
//	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == btnStartAgent)
		{
			Object[] params = new Object[3];
			params[0] = rcvList;
			params[1] = rcList;
			params[2] = rcuList;
			mbpManagerGui.myAgent.StartAgent(txtAgentClass.getText(), txtClient.getText(), params);
			
			rcvList = new ArrayList<ResourceValidation>();
			rcList = new ArrayList<Resource>();
			rcuList = new ArrayList<ResourceUtility>();
			validationListPanel.reloadListObj(rcvList);
			resourceListPanel.reloadListObj(rcList);
			utilityListPanel.reloadListObj(rcuList);
			
			return;
		}
		
		if(e.getSource() == btnCreateRc)
		{			
			ResourceType resourceType = rtb.GetResourceType((String)cmbType.getSelectedItem());
			Resource rc = new Resource(resourceType);
			setDefaultValues(rc);
			
			addResource(rc);
		}
		
		if(e.getSource() == btnCreateRcv)
		{
			ResourceType resourceType = rtb.GetResourceType((String)cmbType.getSelectedItem());
			ResourceValidation rcv = new ResourceValidation(resourceType);
			addResourceValidation(rcv);
		}
		
		if(e.getSource() == btnCreateRcu)
		{
			ResourceType resourceType = rtb.GetResourceType((String)cmbType.getSelectedItem());
			ResourceUtility rcu = new ResourceUtility(resourceType);
			addResourceUtility(rcu);
		}
		
	}
}
